/*
 * @Author: wangwei wwdqq7@qq.com
 * @Date: 2025-04-01 14:20:00
 * @LastEditors: wangwei wwdqq7@qq.com
 * @LastEditTime: 2025-04-01 14:12:29
 * @FilePath: /FullStack/pro/typeorm-mysql/src/user/role.controller.ts
 * @Description: 角色控制器，提供角色管理的API接口
 * - 角色的CRUD操作
 * - 角色权限管理功能
 * - 获取拥有特定角色的用户
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  ParseIntPipe,
  Post,
  Put,
} from '@nestjs/common';
import {
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';

import { CreateRoleDto, RoleResponseDto, UpdateRoleDto } from '@/dto/role.dto';
import { RoleService } from '@/user/role.service';

@ApiTags('角色管理')
@Controller('role')
export class RoleController {
  constructor(private roleService: RoleService) {}

  @ApiOperation({
    summary: '获取所有角色',
    description: '获取系统中所有角色的列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取角色列表',
    type: [RoleResponseDto],
  })
  @Get()
  async getRoles() {
    return this.roleService.getRoles();
  }

  @ApiOperation({
    summary: '获取指定角色',
    description: '根据角色ID获取角色信息',
  })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({
    status: 200,
    description: '成功获取角色信息',
    type: RoleResponseDto,
  })
  @Get(':id')
  async getRoleById(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.getRoleById(id);
  }

  @ApiOperation({ summary: '创建新角色', description: '创建一个新的角色' })
  @ApiBody({ type: CreateRoleDto })
  @ApiResponse({
    status: 201,
    description: '角色创建成功',
    type: RoleResponseDto,
  })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createRole(@Body() roleData: CreateRoleDto) {
    return this.roleService.createRole(roleData);
  }

  @ApiOperation({ summary: '更新角色信息', description: '更新指定角色的信息' })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiBody({ type: UpdateRoleDto })
  @ApiResponse({
    status: 200,
    description: '角色信息更新成功',
    type: RoleResponseDto,
  })
  @Put(':id')
  async updateRole(
    @Param('id', ParseIntPipe) id: number,
    @Body() roleData: UpdateRoleDto,
  ) {
    return this.roleService.updateRole(id, roleData);
  }

  @ApiOperation({ summary: '删除角色', description: '删除指定的角色' })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({ status: 200, description: '角色删除成功' })
  @Delete(':id')
  async deleteRole(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.deleteRole(id);
  }

  @ApiOperation({
    summary: '获取拥有角色的用户',
    description: '获取拥有指定角色的所有用户',
  })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({ status: 200, description: '成功获取用户列表' })
  @Get(':id/users')
  async getRoleUsers(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.getRoleUsers(id);
  }

  @ApiOperation({
    summary: '获取角色权限',
    description: '获取指定角色拥有的所有权限',
  })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({ status: 200, description: '成功获取权限列表' })
  @Get(':id/permissions')
  async getRolePermissions(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.getRolePermissions(id);
  }

  @ApiOperation({
    summary: '分配权限给角色',
    description: '为指定角色分配一个权限',
  })
  @ApiParam({ name: 'roleId', description: '角色ID' })
  @ApiParam({ name: 'permissionId', description: '权限ID' })
  @ApiResponse({ status: 200, description: '权限分配成功' })
  @Post(':roleId/permissions/:permissionId')
  async assignPermissionToRole(
    @Param('roleId', ParseIntPipe) roleId: number,
    @Param('permissionId', ParseIntPipe) permissionId: number,
  ) {
    return this.roleService.assignPermissionToRole(roleId, permissionId);
  }

  @ApiOperation({
    summary: '移除角色权限',
    description: '移除指定角色的一个权限',
  })
  @ApiParam({ name: 'roleId', description: '角色ID' })
  @ApiParam({ name: 'permissionId', description: '权限ID' })
  @ApiResponse({ status: 200, description: '权限移除成功' })
  @Delete(':roleId/permissions/:permissionId')
  async removePermissionFromRole(
    @Param('roleId', ParseIntPipe) roleId: number,
    @Param('permissionId', ParseIntPipe) permissionId: number,
  ) {
    return this.roleService.removePermissionFromRole(roleId, permissionId);
  }
}
